﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace LifeTree.DomainModel.Entities
{
    public class Entity
    {
        internal Entity()
        {
            Id = GenerateId();
        }

        [Key]
        public string Id { get; set; }

        public bool IsValid
        {
            get
            {
                var errors = Validate();
                if (errors.Count == 0)
                    return true;
                return false;
            }
        }

        public IList<ValidationResult> Validate()
        {
            var validationResults = new List<ValidationResult>();
            var ctx = new ValidationContext(this, null, null);
            Validator.TryValidateObject(this, ctx, validationResults, true);
            return validationResults;
        }

        public static string GenerateId()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= ((int)b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }
    }
}
